package com.dgut.lab.controller;

import com.dgut.lab.security.SecurityUserDetails;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class PageController {

    // 前台-登录界面
    @RequestMapping("/login")
    public String userLogin() {
//        System.out.println("进入前台登录");
        return "signIn";
    }

    @PreAuthorize(value = "hasRole('ROLE_STUDENT') or hasRole('ROLE_ADMIN')")
    @RequestMapping("/pageController")
    public void pageTo(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
//        System.out.println(request.isUserInRole("ROLE_ADMIN"));
        if(request.isUserInRole("ROLE_ADMIN"))
            request.getRequestDispatcher("/dashboard").forward(request,response);
        else request.getRequestDispatcher("/submit").forward(request,response);
    }


    @PreAuthorize(value = "hasRole('ROLE_STUDENT')")
    @RequestMapping("/submit")
    public String submitPage(Model model) {
//        System.out.println("进入打卡页面");
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        SecurityUserDetails securityUserDetails = (SecurityUserDetails) authentication.getPrincipal();
        model.addAttribute("userInfo",securityUserDetails.getUserInfo());
        return "submit";
    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @GetMapping("/dashboard")
    public  String dashboard(){
        return "dashboard";
    }

//    @RequestMapping("/user")
//    public String userIndex(Model model) throws JsonProcessingException {
//        RestTemplate rest = new RestTemplate();
//        ObjectMapper objectMapper = new ObjectMapper();
//        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//
//        SecurityUserDetails dgutUserInfo = (SecurityUserDetails) authentication.getPrincipal();
//        model.addAttribute("dgutUserInfo",dgutUserInfo);
//        System.out.println("输出user"+dgutUserInfo);
//        System.out.println("当前权限："+dgutUserInfo.getAuthorities());
//        return "userPage";
//    }
//
//    @ResponseBody
//    @RequestMapping("/users")
//    public Map<String,Object> curruser(){
//        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
////            GiteeUserDetail giteeUserDetail = new GiteeUserDetail();
////            User user = (User) authentication.getPrincipal();
//        SecurityUserDetails dgutUserInfo = (SecurityUserDetails) authentication.getPrincipal();
//        System.out.println("输出user"+dgutUserInfo);
//        System.out.println("当前权限："+dgutUserInfo.getAuthorities());
//        HashMap<String, Object> hashMap = new HashMap<>();
//        hashMap.put("data",dgutUserInfo);
//        return hashMap;
//    }
}
